package leetcode;


/**
 * 最长公共前缀
 */
public class LeetCode14 {
    public String longestCommonPrefix(String[] strs) {
        StringBuilder result = new StringBuilder();
        if (strs.length == 0) return result.toString();
        if (strs.length == 1) return strs[0];
        int index = 0;
        while (true) {
            if (index == strs[0].length()) return result.toString();
            char current = strs[0].charAt(index);
            for (int i = 1; i < strs.length; i++) {
                if (index == strs[i].length() || strs[i].charAt(index) != current) return result.toString();
            }
            result.append(current);
            index++;
        }
    }

    public static void main(String[] args) {
        LeetCode14 leetCode14 = new LeetCode14();
        String[] strs = new String[]{"abc", "a","acd"};
        System.out.println(leetCode14.longestCommonPrefix(strs));
    }

}
